/**
 * Auth Store
 */
import { defineStore } from "pinia";
import { StoreID } from "@/config";
import { persistedStateConfig } from "@/config";
import { useAxiosApi } from "@/api";
import utils from "@/utils";

const api = useAxiosApi();

/**
 * 授权状态存储
 */
export const useAuthStore = defineStore(StoreID.AUTH_STORE, {
    state: (): AuthState => {
        return {
            routeName: "",
            menuList: [],
            buttonList: {},
        };
    },

    getters: {
        /** 扁平化菜单列表 */
        FlatMenuList: (state) => utils.getFlatMenuList(state.menuList) as MenuOptions[],
        /** 显示菜单列表 */
        ShowMenuList: (state) => utils.getShowMenuList(state.menuList) as MenuOptions[],
        /** 面包屑列表 */
        BreadcrumbList: (state) => utils.getAllBreadcrumbList(state.menuList) as MenuOptions[],
    },

    actions: {
        /**
         * 调用api接口，根据身份验证构造对应的菜单列表
         */
        async buildMenuList() {
            const { data } = /*await*/ api.auth_menu_list_local();
            this.menuList = data;
        },

        /** 存储 routerName 做按钮权限筛选 */
        setRouteName(val: string) {
            this.routeName = val;
        },
    },

    // persist: true
});

function convertMenuList(menuList: MenuOptions[]) {
    let newMenuList: MenuOptions[] = [];
    return newMenuList;
}
